package it.polimi.swKnights.SWIMv2.servlets.search;

import it.polimi.swKnights.SWIMv2.ejb.searchEngine.AdvancedSearchRemote;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AdvancedSearchProceed
 */
public class AdvancedSearchProceed extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{

		String surname = request.getParameter("surname");
		String name = request.getParameter("name");
		String ability = request.getParameter("ability");
		String email = request.getParameter("email");
		String city = request.getParameter("city");
		
		String query = "surname=" + surname + "&name=" + name + "&ability="
				+ ability + "&email=" + email + "&city=" + city;
		
		String sumParam = surname + name + ability + email + city;
		if (sumParam == null || sumParam.length() == 0)
		{
			request.getRequestDispatcher("WEB-INF/search/NormalSearch.jsp").forward(request,
					response);
			return;
		}
		
		AdvancedSearchRemote adr = this.lookUp(AdvancedSearchRemote.class);

		List<Map<String, String>> listResultMaps = adr
				.getListOfMapFromGeneralQuery(surname, name, ability, email, city);
		
		request.setAttribute("query", query);
		request.setAttribute("userResultMap", listResultMaps);
		request.getRequestDispatcher("WEB-INF/search/NormalSearch.jsp").forward(request,
				response);

	}

}
